
"""
Provides abstraction layer for interacting with database holding
information about pages.  Currently, this abstraction barrier is not
well defined, and SQL lives in various scripts.  The only thing
solidly inside this abstraction is connecting to the DB.  If this
project expands, we can refactor later.
"""
# $Id: wikinbitDb.py 20 2011-07-29 16:12:39Z postshift@gmail.com $

import os
import sys
import psycopg2
import traceback

if not 'DBHOST' in os.environ:
    sys.exit("DBHOST not found in environment variables.")
else:
    print "Connecting to wikinbitDb at '%s'" % os.environ['DBHOST']

if not 'DBPASS' in os.environ:
    sys.exit("DBPASS not found in environment variables.")

def connect():
    global conn
    try:
        conn = psycopg2.connect("""
            dbname='wikinbit' user='wikinbit' host='%s' password='%s'
            """ % (os.environ['DBHOST'], os.environ['DBPASS']))
    except Exception, exc:
        sys.exit("Failed to connect to wikinbitDb:\n" \
                     + traceback.format_exc(exc))

def reset():
    global conn
    conn.close()
    connect()

connect()
